package org.example.memora.service.serviceImpl;

import org.example.memora.entity.ChatMessage;
import org.example.memora.mapper.ChatMessageMapper;
import org.example.memora.service.ChatMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatMessageServiceImpl implements ChatMessageService {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private ChatMessageMapper chatMessageMapper;
    @Override
    public List<ChatMessage> getHistory(Long communityId, Integer page, Integer size) {
        int offset = (page - 1) * size;
        return chatMessageMapper.selectByCommunityId(communityId, offset, size);
    }
    @Override
    public List<ChatMessage> getHistoryByPage(Long communityId, Integer page, Integer size) {
        int offset = (page - 1) * size;
        return chatMessageMapper.selectByCommunityId(communityId, offset, size);
    }



    @Override
    public void broadcastToCommunity(ChatMessage message) {
        messagingTemplate.convertAndSend(
                "/topic/chat/" + message.getCommunityId(),
                message
        );
    }

    @Override
    public void save(ChatMessage message) {
        chatMessageMapper.insert(message);
    }

//    @Override
//    public List<ChatMessage> getHistory(Long communityId) {
//        return chatMessageMapper.selectByCommunityId(communityId);
//    }
}
